package agents.persons.leader;

import org.apache.log4j.Logger;

import com.avanti.routes.ListaCuadrantes;

import agents.messages.MessageConstants;
import agents.persons.dependent.Dependent;
import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import log.Action;
import log.ContadorEventos;

public class ManageLeadership extends TickerBehaviour{

	private static final long serialVersionUID = -8992518927055044887L;
	private Logger logger = Logger.getLogger(LeaderAgent.class);

	public ManageLeadership(Agent a, long period) {
		super(a, period);
	}


	protected void onTick() {
		MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.INFORM);
		ACLMessage msg = myAgent.receive(mt);
		
		if (msg != null){
			String idCuad = Integer.toString((((LeaderAgent) myAgent).getCuadranteActual()));
			logger.info(ContadorEventos.addEvent(idCuad, Action.RECIBIR_MSJ, msg.getContent()));
			//Se ha recibido un mensaje que trata de liderazgo. Tratar.
			if (msg.getContent().equals(MessageConstants.MSJ_HOLA_LIDER)){
				////System.out.println("Al agente "+myAgent.getLocalName()+" le sigue otra persona:). Aumenta liderazgo");
				((LeaderAgent)myAgent).aumentaLiderazgo();
				((LeaderAgent)myAgent).addSeguidor(msg.getSender());
				
			}
			else if (msg.getContent().equals(MessageConstants.MSJ_ADIOS_LIDER)){
				////System.out.println("Al agente "+myAgent.getLocalName()+" le sigue uno menos :(. Aunmenta liderazgo");
				((LeaderAgent)myAgent).disminuyeLiderazgo();
			}
		}
		else
			block();
	}

}
